package com.yuni.index.utils;

import org.springframework.stereotype.Component;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

@Component
public class RedisUtil {
    private final JedisPool jedisPool;

    public RedisUtil() {
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        this.jedisPool = new JedisPool(poolConfig, "localhost", 6379, 2000, "Gsy050416@");
    }

    public void saveCode(String email, String code) {
        try (redis.clients.jedis.Jedis jedis = jedisPool.getResource()) {
            jedis.setex("verification_code:" + email, 300, code); // 设置5分钟过期时间
        }
    }

    public String getCode(String email) {
        try (Jedis jedis = jedisPool.getResource()) {
            return jedis.get("verification_code:" + email);
        }
    }
}
